package com.ai.aicontact.busi.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ai.aicontact.busi.entity.RedBlackList;
import com.ai.aicontact.busi.service.RedBlackListService;
import com.ai.aicontact.common.vo.PageFilter;
import com.ai.aicontact.core.dao.BaseDaoI;


@Service
public class RedBlackListServiceImpl implements RedBlackListService{

	 @Autowired
	 BaseDaoI<RedBlackList> redBlackListDao;
	
	@Override
	public void saveRedAndBlackList(RedBlackList redBlackList) throws Exception {
		
		if (redBlackList != null) {
			redBlackList.setState(1);
			redBlackList.setDoneCode(11l);
			redBlackList.setCreateDate(new Date(System.currentTimeMillis()));
			redBlackList.setOpId("123456");
			redBlackListDao.save(redBlackList);
		}
	}

	@Override
	public List<RedBlackList> queryRedBlackList(String keyWord,RedBlackList redBlackList, PageFilter pf) throws Exception{
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder hql = new StringBuilder("from RedBlackList ");
		hql.append(whereHql(keyWord,redBlackList, params));
		hql.append(orderHql(pf)); 
		List<RedBlackList> list = redBlackListDao.find(hql.toString(), params, pf.getPageNumber(), pf.getPageSize());
		return list;

	}

	private String whereHql(String keyWord,RedBlackList redBlackList, Map<String, Object> params) {
		StringBuilder hql = new StringBuilder(" where 1=1 And state=1 ");
		
		//表示用户使用手机号码查询
		if (!("".equals(redBlackList.getBillId())) && redBlackList.getBillId() != null) {
			hql.append(" AND billId like :billId");
			params.put("billId", "%"+redBlackList.getBillId()+"%");
		}
		//表示用户使用的名单的类型查找
		if (redBlackList.getType() != null) {
			if (redBlackList.getType()==1 || (redBlackList.getType()==2)) {
				hql.append(" AND type = :type");
				params.put("type", redBlackList.getType());
			}
		}
		return hql.toString();
	}
	
	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by " + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}

	
	@Override
	public Long countRedBlackList(String keyWord,RedBlackList RedBlackList) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder hql = new StringBuilder("select count(*) from RedBlackList ");
		hql.append(whereHql(keyWord,RedBlackList, params));
		Long total = redBlackListDao.count(hql.toString(), params);
		return total;
	}

	@Override
	public void deleteRedBlackList(RedBlackList redBlackList) throws Exception {
		
		if (redBlackList != null) {
			RedBlackList redBlackList2 = redBlackListDao.get(RedBlackList.class, redBlackList.getId());
			redBlackListDao.delete(redBlackList2);
			
		}
	}
	

}
